package com.levi.threadutils;


import com.levi.threadutils.domain.User;
import com.levi.threadutils.service.UserService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * @Author Honey
 * @Date 2019/11/15 10:31
 * @Description
 **/
@Slf4j
public class UserThread1 implements Callable<Integer>{

    private Integer startRow;

    private Integer endRow;

    private UserService userService;

    private CountDownLatch latch;

    private List<User> userList;

    public UserThread1(List<User> userList, Integer startRow, Integer endRow, UserService userService, CountDownLatch latch) {
        this.userList = userList;
        this.startRow = startRow;
        this.endRow = endRow;
        this.userService = userService;
//        this.semaphore = semaphore;
        this.latch = latch;
    }

    @Override
    public Integer call() throws Exception {
        log.info("线程ID：<{}>开始运行,startRow:{},endRow:{}",Thread.currentThread().getId(),startRow,endRow);
        latch.countDown();
        int count = 0;
        List<User> userList = this.userList.subList(startRow-1, endRow);
        Integer insertNum = userService.insertDB(userList);
        return count+insertNum;
    }
}

